package com.recyclerviewdemo.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.recyclerviewdemo.R;
import com.recyclerviewdemo.entity.GankResult;
import com.recyclerviewdemo.utils.Utils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Yang on 2016/9/5.
 */
public class AndroidAdapter extends RecyclerView.Adapter<AndroidAdapter.AndroidViewHolder>{

    private Context context;
    private List<GankResult.ResultsBean> picData = new ArrayList<>();
    private List<GankResult.ResultsBean> androidData = new ArrayList<>();

    public AndroidAdapter(Context context){
        this.context = context;
    }

    public List<GankResult.ResultsBean> getPicData() {
        return picData;
    }

    public void setPicData(List<GankResult.ResultsBean> picData) {
        this.picData = picData;
    }

    public List<GankResult.ResultsBean> getAndroidData() {
        return androidData;
    }

    public void setAndroidData(List<GankResult.ResultsBean> androidData) {
        this.androidData = androidData;
    }

    @Override
    public AndroidViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recv_android,parent,false);
        AndroidViewHolder vh = new AndroidViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(AndroidViewHolder holder, int position) {
        holder.setDataToView(picData.get(position), androidData.get(position));
    }


    @Override
    public int getItemCount() {
        return androidData.size();
    }

    class AndroidViewHolder extends RecyclerView.ViewHolder{
        private TextView txv_desc;
        private TextView txv_imgauthor;
        private TextView txv_author;
        private ImageView img_meizi;

        public AndroidViewHolder(View itemView) {
            super(itemView);
            txv_author = (TextView)itemView.findViewById(R.id.txt_goods_author);
            txv_desc = (TextView)itemView.findViewById(R.id.txt_goods_title);
            img_meizi = (ImageView)itemView.findViewById(R.id.img_goods_img);
            txv_imgauthor = (TextView)itemView.findViewById(R.id.txt_img_author);
        }

        public void setDataToView(GankResult.ResultsBean picdata,GankResult.ResultsBean androiddata){
            txv_desc.setText(androiddata.getDesc());
            txv_imgauthor.setText(picdata.getWho());
            Date date = Utils.formatDateFromStr(androiddata.getPublishedAt());
            String dateStr = Utils.getFormatDateStr(date);
            txv_author.setText(androiddata.getWho() + "@" + dateStr);
            Picasso.with(context)
                    .load(picdata.getUrl())
                    .error(R.drawable.meizi)
                    .fit()
                    .centerCrop()
                    .into(img_meizi);
        }
    }
}
